/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package it.mdc.hearts.war.room;

import it.mdc.hearts.war.room.core.GameTable;
import it.mdc.hearts.war.room.core.UserInfo;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import javax.faces.bean.ApplicationScoped;
import javax.faces.bean.ManagedBean;
import org.primefaces.push.PushContext;
import org.primefaces.push.PushContextFactory;

/**
 *
 * @author Administrator
 */
@ManagedBean(name = "mainRoomBean")
@ApplicationScoped
public class MainRoomBean implements Serializable{

    private final Map<Integer, UserInfo> userMap = Collections.synchronizedMap(new HashMap<Integer, UserInfo>());
    private final Map<UUID, GameTable> tableMap = Collections.synchronizedMap(new HashMap<UUID, GameTable>());
    
    private final PushContext pushContext = PushContextFactory.getDefault().getPushContext();
    private final String CHANNEL = "/refreshPage";
    
    private List<UserInfo> userList;
    private List<GameTable> gameTableList;
   
    void userJoinRoom(Integer ID, UserInfo userInfo) {
        userMap.put(ID, userInfo);
        pushContext.push(CHANNEL, "refresh");
    }
    
    void userRemoveRoom(Integer ID) {
        userMap.remove(ID);
        pushContext.push(CHANNEL, "refresh");
    }
    
    void addGameTable(UUID tableID, GameTable table) {
        tableMap.put(tableID, table);
        pushContext.push(CHANNEL, "refresh");
    }
    
    void removeGameTable(UUID tableUUID) {
        tableMap.remove(tableUUID);
        pushContext.push(CHANNEL, "refresh");
    }

    public List<UserInfo> getUserList() {
        userList = new ArrayList<UserInfo>(userMap.values());
        return userList;
    }
    
     public List<GameTable> getGameTableList() {
        gameTableList = new ArrayList<GameTable>(tableMap.values());
        Collections.sort(gameTableList);
        return gameTableList;
    }
}
